package com.qianxx.component.activity.image.swipeable_card;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.qianxx.component.R;
import com.qianxx.viewlibrary.image.swipeablecards.SwipeFlingAdapterView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class SwipeableCardActivity extends AppCompatActivity {
    @BindView(R.id.swipe_fling_adapter_view)
    SwipeFlingAdapterView swipe_fling_adapter_view;

    private int i;
    private List<Bitmap> list = new ArrayList<>();
    private SwipeableCardsAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_swipeable_card);
        ButterKnife.bind(this);

        initData();

        adapter = new SwipeableCardsAdapter();
        swipe_fling_adapter_view.setAdapter(adapter);
        swipe_fling_adapter_view
                .setFlingListener(new SwipeFlingAdapterView.onFlingListener() {
                    @Override
                    public void removeFirstObjectInAdapter() {
                        // this is the simplest way to delete an object from the
                        // Adapter (/AdapterView)
                        list.remove(0);
                        adapter.notifyDataSetChanged();
                    }

                    @Override
                    public void onLeftCardExit(Object dataObject) {
                        // makeToast(MainActivity.this, "Left!");
                    }

                    @Override
                    public void onRightCardExit(Object dataObject) {
                        // makeToast(MainActivity.this, "Right!");
                    }

                    @Override
                    public void onAdapterAboutToEmpty(int itemsInAdapter) {
                        // Ask for more data here
                        // list.add("XML ".concat(String.valueOf(i)));
                        // adapter.notifyDataSetChanged();
                        // i++;

                    }

                    @Override
                    public void onScroll(float scrollProgressPercent) {
                    }
                });
        swipe_fling_adapter_view
                .setOnItemClickListener(new SwipeFlingAdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClicked(int itemPosition,
                                              Object dataObject) {

                    }
                });
    }

    private void initData() {

        Bitmap bmp1 = BitmapFactory.decodeResource(getResources(),
                R.drawable.p1);
        Bitmap bmp2 = BitmapFactory.decodeResource(getResources(),
                R.drawable.p2);
        Bitmap bmp3 = BitmapFactory.decodeResource(getResources(),
                R.drawable.p3);
        Bitmap bmp4 = BitmapFactory.decodeResource(getResources(),
                R.drawable.p4);
        Bitmap bmp5 = BitmapFactory.decodeResource(getResources(),
                R.drawable.p5);
        Bitmap bmp6 = BitmapFactory.decodeResource(getResources(),
                R.drawable.p6);
        Bitmap bmp7 = BitmapFactory.decodeResource(getResources(),
                R.drawable.p7);
        Bitmap bmp8 = BitmapFactory.decodeResource(getResources(),
                R.drawable.p8);

        list.add(bmp1);
        list.add(bmp2);
        list.add(bmp3);
        list.add(bmp4);
        list.add(bmp5);
        list.add(bmp6);
        list.add(bmp7);
        list.add(bmp8);
    }

    class SwipeableCardsAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder vh = null;
            if (convertView == null) {
                convertView = LayoutInflater.from(getApplicationContext())
                        .inflate(R.layout.item_swipeable_card, null);
                vh = new ViewHolder();
                vh.image = (ImageView) convertView.findViewById(R.id.image);
                convertView.setTag(vh);
            } else {
                vh = (ViewHolder) convertView.getTag();
            }

            vh.image.setImageBitmap(list.get(position));
            vh.image.setImageBitmap(list.get(position));
            return convertView;
        }

        class ViewHolder {
            ImageView image;
        }

    }
}
